package com.dream.inspect.modules.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.common.validator.group.AddGroup;
import com.dream.inspect.modules.business.dao.SuperviseImageDao;
import com.dream.inspect.modules.business.entity.SuperviseImageEntity;
import com.dream.inspect.modules.business.service.SuperviseImageService;


@Service("superviseImageService")
public class SuperviseImageServiceImpl extends ServiceImpl<SuperviseImageDao, SuperviseImageEntity> implements SuperviseImageService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SuperviseImageEntity> page = this.selectPage(
                new Query<SuperviseImageEntity>(params).getPage(),
                new EntityWrapper<SuperviseImageEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SuperviseImageEntity> findSuperviseImageList(Map<String, String> params) {
        List<SuperviseImageEntity> sqrList = new ArrayList<SuperviseImageEntity>();
        //组装条件
        EntityWrapper<SuperviseImageEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseImageList(wrapper);
        return sqrList;
    }
    
    @Override
    public List<SuperviseImageEntity> findSuperviseImageListByWeb(Map<String, String> params) {
        List<SuperviseImageEntity> sqrList = new ArrayList<SuperviseImageEntity>();
        //组装条件
        EntityWrapper<SuperviseImageEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseImageListByWeb(wrapper);
        return sqrList;
    }

    @Override
    public boolean insertSuperviseImage(Map<String, String> params) {
        boolean result = false;
        Date now = new Date();
        String userId = params.get("userId");
        String sqmId = params.get("sqmId");
        String jasonArray = params.get("imageList");
        List<SuperviseImageEntity> siList = new ArrayList<SuperviseImageEntity>();
        if (StringUtils.isNotEmpty(jasonArray)) {
            siList = JSONObject.parseArray(jasonArray, SuperviseImageEntity.class);
        }
        //save
        if (siList != null && siList.size() > 0) {
            for (SuperviseImageEntity temp : siList) {
                if (StringUtils.isNotEmpty(sqmId)) {
                    temp.setWtglId(Long.valueOf(sqmId));
                }
                if (StringUtils.isNotEmpty(userId)) {
                    temp.setCreateUser(Long.valueOf(userId)); 
                    temp.setCreateTime(now);
                    temp.setUpdateUser(Long.valueOf(userId));
                    temp.setUpdateTime(now);
                }
            }
            
            ValidatorUtils.validateEntity(siList, AddGroup.class);
            result = this.insertBatch(siList);
        }
        return result;
    }
    
    /**
     * 拼接查询条件
     * 
     * @param params
     * @return
     */
    private EntityWrapper<SuperviseImageEntity> addCondition(Map<String, String> params) {
        String questionId = params.get("questionId");
        String picType = params.get("picType");
        String qf = params.get("qf");

        EntityWrapper<SuperviseImageEntity> wrapper = new EntityWrapper<SuperviseImageEntity>();
        wrapper.setEntity(new SuperviseImageEntity());
        //问题ID
        if (StringUtils.isNotEmpty(questionId)) {
            wrapper.eq("WTGL_ID", questionId);
        }
        if (StringUtils.isNotEmpty(picType)) {
            wrapper.eq("PIC_TYPE", picType);
        }
        if (StringUtils.isNotEmpty(qf)) {
            wrapper.eq("QF", qf);
        }
    
        return wrapper;
    }

}
